package com.kaigejava.juc.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author kaigejava
 */
public class ReentrantLockDemo {

    static   ReentrantLock lock = new ReentrantLock(true);
    public static void main(String[] args) {
        for(int i = 0;i<3;i++){
            new Thread(()->reentrantLockTest(),"线程"+(i+1)).start();
        }
    }

    public static  void reentrantLockTest(){

        try {
            System.out.println(Thread.currentThread().getName()+"开始获取锁。");
            lock.lock();
            System.out.println(Thread.currentThread().getName()+"获取到了锁。开始做其他的操作了====do.......... ");
            TimeUnit.SECONDS.sleep(2);
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
            System.out.println("======关闭锁=======");
        }

    }


}
